#include "adLogic.h"
#include "adDao.h"
Ad* parseUpDownSplitScr(cJSON *adJson){
    Ad *result = calloc(1,sizeof(Ad));
    if (cJSON_IsObject(adJson))
    {
        //解析广告
        cJSON *idJson = cJSON_GetObjectItemCaseSensitive(adJson,"id");
        int id = idJson->valueint;
        cJSON *templateIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"template_id");
        int templateId = templateIdJson->valueint;
        cJSON *customerIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"customer_id");
        int customerId = customerIdJson->valueint;
        cJSON *startTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"start_time");
        char *startTime = startTimeJson->valuestring;
        cJSON *endTimeJson = cJSON_GetObjectItemCaseSensitive(adJson,"end_time");
        char *endTime = endTimeJson->valuestring;
        result->id = id;
        result->template_id = templateId;
        result->customer_id = customerId; 
        result->start_time = startTime;
        result->end_time = endTime;
        //AdResource *resources[3];
        //广告资源解析
        AdResource *upAr = calloc(1,sizeof(AdResource));
        upAr->ad_id = id;
        upAr->index = 0;
        cJSON *typeIdJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_type_id");
        int typeId = typeIdJson->valueint;
        upAr->type_id = typeId;
        cJSON *urlJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_url");
        char *url = urlJson->valuestring;
        upAr->path = url;
        cJSON *signJson = cJSON_GetObjectItemCaseSensitive(adJson,"top_media_sign");
        char *sign = signJson->valuestring;
        upAr->sign = sign;
        result->adResources[0] = upAr;
        AdResource *buttomAr = calloc(1,sizeof(AdResource));
        buttomAr->ad_id = id;
        buttomAr->index = 0;
        cJSON *typeIdJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_type_id");
        int typeId1 = typeIdJson1->valueint;
        buttomAr->type_id = typeId1;
        cJSON *urlJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_url");
        char *url1 = urlJson1->valuestring;
        buttomAr->path = url1;
        cJSON *signJson1 = cJSON_GetObjectItemCaseSensitive(adJson,"buttom_media_sign");
        char *sign1 = signJson1->valuestring;
        buttomAr->sign = sign1;
        result->adResources[1] = buttomAr;
        result->adResources[2] = NULL;
    }
    return result;
}
char* getResPath(AdResource *ar){
    //获取ar对应的广告id
    int adId = ar->ad_id;
    //获取文件名
    gchar *fileName = g_path_get_basename(ar->path);
    //格式文件
    char *formatStr = "%s/%d_%s";
    return g_strdup_printf(formatStr,RES_PATH,adId,fileName);
}
int downloadAdsAndUpdateLocalAdListToSqlite(Ad* ads[],int size){
    printf("downloadAdsAndUpdateLocalAdListToSqlite....\n");
    //定义一个返回数据的标识，来判断数据是否更新
    int flag = 0;
    //遍历广告数组
    for (int i = 0; i < size; i++)
    {
        //获取广告id
        Ad *ad = ads[i];
        //查询该广告是否在sqlite当中
        int adId = ad->id;
        Ad *adInDb = getAd(adId);
        //不存在
        if (adInDb == NULL)
        {
            flag = 1;
            //将广告添加到sqlite当中
            addAd(ad);
            for (int j = 0; j < 3; j++)
            {
                AdResource *ar = ad->adResources[j];
                if (ar != NULL)
                {
                    //将从远程获取到的路径改为本地路径
                    char *localArPath = getResPath(ar);
                    //判断文件是否存在
                    gboolean exit = g_file_test(localArPath,G_FILE_TEST_EXISTS);
                    //不存在
                    if (!exit)
                    {
                        //创建文件
                        GFile *file = g_file_new_for_path(localArPath);
                        //判断是否创建成功
                        if (!g_file_create(file,0,NULL,NULL))
                        {
                            g_print("create file failed\n");
                        }
                    }
                    //下载
                    downloadRes(ar->path,localArPath);
                    //添加到对应广告资源中
                    ar->path = localArPath;
                    addAdResource(ar);
                }
            }
        }else
        {
            //存在，但是需要更新
            updateAd(ad);
            //判断是否需要重新下载广告资源
            for (int j = 0; j < 3; j++)
            {
                AdResource *ar = ad->adResources[j];
                if (ar != NULL)
                {
                    //将从远程获取到的路径改为本地路径
                    char *localArPath = getResPath(ar);
                    char *sn = ar->sign;
                    char *localSn = "jfjjfjfjj";
                    if (!g_str_equal(sn,localSn))
                    {
                        //判断文件是否存在
                        gboolean exit = g_file_test(localArPath,G_FILE_TEST_EXISTS);
                        //不存在
                        if (!exit)
                        {
                            flag = 1;
                            //创建文件
                            GFile *file = g_file_new_for_path(localArPath);
                            //判断是否创建成功
                            if (!g_file_create(file,0,NULL,NULL))
                            {
                                g_print("create file failed\n");
                            }
                        }
                        //下载
                        downloadRes(ar->path,localArPath);
                        //添加到对应广告资源中
                        ar->path = localArPath;
                        addAdResource(ar);
                    }
                }
            }
        }
    }
    return flag;
}
void handleAdList(cJSON *adListArray){
    printf("handleAdList1.....\n");
    //判断adListArray是否为数组
    if (!cJSON_IsArray(adListArray))
    {
        printf("handleAdList2.....\n");
        return;
        printf("handleAdList3.....\n");
    }
    printf("handleAdList4.....\n");
    //将解析出的数据存放到数组中
    int size = cJSON_GetArraySize(adListArray);
    Ad *ads[size];
    printf("handleAdList5.....\n");
    for (int i = 0; i < size; i++)
    {
        //获取第几个对象
        cJSON *adJson = cJSON_GetArrayItem(adListArray,i);
        //获取模版的类型
        int templateId = cJSON_GetObjectItemCaseSensitive(adJson,"template_id")->valueint;
        switch (templateId)
        {
        case 6:
            Ad *ad = parseUpDownSplitScr(adJson);
            ads[i] = ad;
            break;
        case 5:
            break;
        default:
        g_print("template_id is invalid\n");
            break;
        }
    }
    printf("handleAdList8.....\n");
    //将下载的广告资源和本地广告列表存入sqlite
    int res = downloadAdsAndUpdateLocalAdListToSqlite(ads,size);
    printf("handleAdList9.....\n");
    //释放资源
    for (int i = 0; i < size; i++)
    {
        Ad *ad =  ads[i];
        g_print("adid = %d\n",ad->id);
        for (int i = 0; i < 3; i++)
        {
            AdResource *ar = ad->adResources[i];
            if (ar!=NULL)
            {
                printf("handleAdList14.....\n");
                g_print("url=%s\n",ar->path);
                free(ar);
            }
        }
        free(ad);
    }
    // delExpiredAds();
    if (res == 1)
    {
        // g_print("send msg to QT process!\n");
    }else
    {
        // g_print("all ad lastest!\n");
    }
}
void *parseCmd(cJSON *cmdList){
    //判断
    if (!cJSON_IsArray(cmdList))
    {
        return NULL;
    }
    //获取数组size
    int size = cJSON_GetArraySize(cmdList);
    for (int i = 0; i < size; i++)
    {
        //获取数组元素
        cJSON *item = cJSON_GetArrayItem(cmdList,i);
        int cmd = item->valueint;
        switch (cmd)
        {
        case 1:
            //关机
            //system(" echo yaosang | sudo -S shutdown -h now "); 
            break;
        case 2:
            //重启
            //system("echo yaosang | sudo -S reboot");
            break;
        case 3:
            //检查更新
            break;
        default:
            break;
        }
    }
}